package it.codelair.bakekaservice;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class AskAccount extends Activity implements OnClickListener{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ask_account);
		// Show the Up button in the action bar.
		setupActionBar();
		AccountManager accountMgr = (AccountManager) getSystemService(ACCOUNT_SERVICE);
    	Account[] accounts = accountMgr.getAccountsByType("it.codelair.bakekaservice");
    	if(accounts!=null && accounts.length>0){
    		Intent home = new Intent(getApplicationContext(), PrincipaleActivity.class);
            startActivity(home);
    	}
    	else{
			final Button buttonLogin = (Button) findViewById(R.id.buttonLogin);
			buttonLogin.setOnClickListener(this);
			final Button buttonRegister = (Button) findViewById(R.id.buttonRegister);
			buttonRegister.setOnClickListener(this);
    	}
	}
	
	public void onClick(View v) {
		if(checkConn()){
            switch ( v.getId() ) {
	            case R.id.buttonLogin:
	            	Intent login = new Intent(getApplicationContext(), LoginActivity.class);
	            	startActivity(login);
	            	break;
	            case R.id.buttonRegister:
	            	Intent register = new Intent(getApplicationContext(), Register_activity.class);
	            	startActivity(register);
	            	break;
	            }
            this.finish();
		}
		else {
			Toast t = Toast.makeText(v.getContext(), "Connessione internet non disponibile!", Toast.LENGTH_SHORT);
			t.show();
		}
	}
	
	public boolean checkConn()
    {
        ConnectivityManager conMgr =  (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo i = conMgr.getActiveNetworkInfo();
        if (i == null)
          return false;
        if (!i.isConnected())
          return false;
        if (!i.isAvailable())
          return false;
        return true;
    }

	/**
	 * Set up the {@link android.app.ActionBar}.
	 */
	private void setupActionBar() {

		getActionBar().setDisplayHomeAsUpEnabled(true);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.ask_account, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
//	@Override
//	public void onResume(){
//		AccountManager accountMgr = (AccountManager) getSystemService(ACCOUNT_SERVICE);
//    	Account[] accounts = accountMgr.getAccountsByType("it.codelair.bakekaservice");
//    	if(accounts!=null && accounts.length>0){
//    		Intent home = new Intent(getApplicationContext(), Splash_activity.class);
//            startActivity(home);
//    	}
//    	else{
//    		super.onResume();
//    	}
//	}
	

}
